<?php
namespace app\admin\controller;
class Buycar extends Base
{
    //列表
    public function lists(){
        if(is_post()){
            $d = $this->post;
            $map = [];
            if($d['sh_keyword']) $map[] = ['and','khname|addressName|addressTel','like',"%{$d['sh_keyword']}%"];
            if($d['title'])   $map[] = ['and','goodsName','like',"%{$d['title']}%"];
            if($d['pay_status']!='') $map[] = ['and','workFlow','=',$d['pay_status']];
            if($d['min_time']!='') $map[] = ['and','time','>=',$d['min_time']];
            if($d['max_time']!='') $map[] = ['and','time','<=',$d['max_time']];
            $w  = where($map);
            $sql = "select * from buycar $w[0] order by time desc".limit($d['page'],$d['limit']);
            $rs = $this->db->query($sql, $w[1],'',true);

            if($rs){
              suc($rs[0],$rs[1]);
            }else{
                suc([],0);
            }
        }
        $this->fetch();
    }

    //发货
    public function edit(){
      if(is_post()){
        $d = $this->post;
        $order_no = $d["order_no"];

        //快递公司
        $info = $this->db->query("select * from express where id = ?",[$d['kd_id']],1);
        // $d['kd_name'] = $info['name'];
        // $d['kd_code'] = $info['kuaidi_no'];
        $data = [
            'workFlow'=>'已发货',
            'expressNum'=>$d['kd_no'],
            'expressName'=>$info['name'],
            'text'=>$d['kd_note'],
        ];
        $list = $this->db->query("select * from buycar where id=?",[$order_no],1);
        $rs = $this->db->update("buycar",$data,['id=?',[$order_no]]);
        $msg = "旧订单发货,内容为:" . json_encode($list,JSON_UNESCAPED_UNICODE);
        //添加日志
        if($rs){
            m('Log','xcom')->record($this->userinfo["id"], $msg,'admin_user');
            cookie('js_msg','发货成功');
            cookie('js_function','preload');
            return suc();
        }else{
            return err("发货失败");
        }
      }
      //快递公司列表
      $elist = $this->db->query("select * from express where status = ? order by id asc",[0]);
      $order_no = $this->get("order_no");
      $info = $this->db->query("select * from buycar where id=?",[$order_no],1);
      $this->fetch(['info'=>$info,'elist'=>$elist]);
    }

}
